package breeze.pixel.weather.main_view.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.IOException;
import java.util.List;

import breeze.pixel.weather.R;
import breeze.pixel.weather.adapter.weather.IndicesAdapter;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;

public class LifeStyleDataAdapter extends RecyclerView.Adapter<LifeStyleDataAdapter.MyViewHolder> {

    List<IndicesAdapter.DailyBean> data;

    Context mContext;

    private onLifeStyleClickListener listner;

    public LifeStyleDataAdapter(Context context,List<IndicesAdapter.DailyBean> list){
        this.data = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.view_lifestyle_item, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        IndicesAdapter.DailyBean bean = data.get(position);
        String name = bean.getName();
        name=name.substring(0,4);
        holder.tv.setText(String.format("%s：%s", name,bean.getCategory()));
        Bitmap bitmap = getIcon(name);
        holder.img.setImageBitmap(bitmap);
        holder.linearlayout.setOnClickListener(v -> listner.onClick(bean));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        MTextView tv;
        LinearLayout linearlayout;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.imageView);
            tv = itemView.findViewById(R.id.textView);
            linearlayout = itemView.findViewById(R.id.view_lifestyle_linearlayout);
        }
    }

    private Bitmap getIcon(String name){
        try {
            Bitmap bitmap = BitmapFactory.decodeStream(mContext.getAssets().open("lifestyle/"+name+".png"));
            return bitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.m999);
        }
    }

    public void setOnItemClickListner(onLifeStyleClickListener listner){
        this.listner=listner;
    }

    public interface onLifeStyleClickListener{
        void onClick(IndicesAdapter.DailyBean data);
    }
}
